<template>
  <div class="post-card" @click="handleCardClick">
    <div class="post-header">
      <div class="author-info">
        <img 
          :src="post.author.avatar || '/default-avatar.png'" 
          :alt="post.author.nickname"
          class="author-avatar"
        />
        <span class="author-name">{{ post.author.nickname }}</span>
      </div>
      <div class="post-meta">
        <span class="views">👁️ {{ formatNumber(post.views) }}</span>
      </div>
    </div>

    <div class="post-images" v-if="post.images && post.images.length > 0">
      <img 
        v-for="(image, index) in post.images" 
        :key="index"
        :src="image" 
        :alt="post.title"
        class="post-image"
        @click.stop="previewImage(image)"
      />
    </div>

    <div class="post-content">
      <h3 class="post-title">{{ post.title }}</h3>
      <p class="post-text" v-if="post.content">{{ post.content }}</p>
      
      <div class="post-tags" v-if="post.tags && post.tags.length > 0">
        <span 
          v-for="tag in post.tags" 
          :key="tag"
          class="tag"
          @click.stop="$emit('tag-click', tag)"
        >
          #{{ tag }}
        </span>
      </div>
    </div>

    <div class="post-footer">
      <button class="action-btn" @click.stop="handleLike">
        <span>{{ post.likes }}</span>
        <span class="icon">❤️</span>
      </button>
      <button class="action-btn" @click.stop>
        <span>{{ post.comments }}</span>
        <span class="icon">💬</span>
      </button>
      <button class="action-btn" @click.stop>
        <span>{{ post.collects }}</span>
        <span class="icon">⭐</span>
      </button>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const props = defineProps({
  post: {
    type: Object,
    required: true
  }
})

const router = useRouter()

defineEmits(['tag-click'])

// 点击卡片跳转到详情页
const handleCardClick = () => {
  router.push(`/post/${props.post.id}`)
}

const formatNumber = (num) => {
  if (num >= 10000) {
    return (num / 10000).toFixed(1) + 'w'
  } else if (num >= 1000) {
    return (num / 1000).toFixed(1) + 'k'
  }
  return num.toString()
}

const handleLike = () => {
  // TODO: 实现点赞功能
  console.log('点赞', props.post.id)
}

const previewImage = (imageUrl) => {
  // TODO: 实现图片预览功能
  console.log('预览图片', imageUrl)
}
</script>

<style scoped>
.post-card {
  background: #fff;
  border-radius: 0;
  overflow: hidden;
  box-shadow: none;
  border-right: 1px solid #f0f0f0;
  border-bottom: 1px solid #f0f0f0;
  transition: transform 0.2s;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  height: 100%;
}

.post-card:hover {
  transform: scale(1.01);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.post-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-bottom: 1px solid #f0f0f0;
}

.author-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.author-avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  object-fit: cover;
}

.author-name {
  font-size: 12px;
  color: #333;
  font-weight: 500;
}

.post-meta {
  font-size: 12px;
  color: #999;
}

.post-images {
  width: 100%;
  position: relative;
  aspect-ratio: 1;
  overflow: hidden;
}

.post-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  cursor: pointer;
  transition: transform 0.3s;
  display: block;
}

.post-image:hover {
  transform: scale(1.05);
}

.post-content {
  padding: 12px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.post-title {
  margin: 0 0 6px 0;
  font-size: 14px;
  font-weight: 600;
  color: #333;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.post-text {
  margin: 0 0 8px 0;
  font-size: 12px;
  color: #666;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  flex: 1;
}

.post-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 8px;
}

.tag {
  font-size: 10px;
  color: #ff2442;
  background: #fff0f0;
  padding: 2px 6px;
  border-radius: 3px;
  cursor: pointer;
  transition: all 0.2s;
}

.tag:hover {
  background: #ffe0e0;
  color: #e01d37;
}

.post-footer {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  border-top: 1px solid #f0f0f0;
  margin-top: auto;
}

.action-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 12px;
  color: #666;
  padding: 4px 6px;
  border-radius: 4px;
  transition: all 0.2s;
  flex: 1;
  justify-content: center;
}

.action-btn:hover {
  background: #f5f5f5;
  color: #ff2442;
}

.action-btn .icon {
  font-size: 14px;
}

/* 响应式 */
@media (max-width: 768px) {
  .post-content {
    padding: 12px;
  }

  .post-title {
    font-size: 15px;
  }

  .post-text {
    font-size: 13px;
  }
}
</style>

